﻿using MediatR;
using Sowrk03_28.Repository;
using Swork03_28.Api.Application.Commands.FarmedCommand;
using Swork03_28.Api.Application.Commands.TraceableCommand;
using Swork03_28.Domain.TraceableInfos;
using Swork03_28.Services.Interface;

namespace Swork03_28.Api.Application.CommandHandlers.FarmedCommand
{
    public class VaccineDelImgCommandHandler : IRequestHandler<VaccineDelImgCommand, int>
    {
        private readonly IRepository<Farmed> _Farmed;

        public VaccineDelImgCommandHandler(IRepository<Farmed> Farmed)
        {
            this._Farmed = Farmed;
        }

        public async Task<int> Handle(VaccineDelImgCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var info = await _Farmed.GetValues(x => x.BatchId == request.BatchId);
            var Imgs = info.FirstOrDefault().Certificate.Split(",").ToList();
            Imgs.RemoveAll(x => x == request.Img);
            info.FirstOrDefault().Certificate = string.Join(",", Imgs);
            count += await _Farmed.UpdateAsync(info.FirstOrDefault());
            return count;
        }
    }
}
